<?php
// ====================================

// Author:            韩孝龙
// Email:             18301805881@163.com
// Created Time:      2020-11-05 22:10:43
// Description:

// ====================================

class Common {
    /**
     * 是否包含所需参数
     * @param array $need - 所需参数
     * @param array $param - 实际传参
     * @return bool
     */
    public static function checkParamExist(array $need, array $param) {
        foreach($need as $v) {
            if (!isset($param[$v])) {
                return false;
            }
        }

        return true;
    }

    // 检查接口的签名
    public static function checkSign($param) {
        $signClient = $param['sign'];

        if (conGame::DEVELOP_MODE && strlen($signClient) !== 32) {
            return conError::ERR_OK;
        }

        if (!self::checkParamExist(['param', 'sign'], $param)) {
            Log::error(__METHOD__ . ', invalid param, param = ' . json_encode($param));
            return conError::ERR_INVALID_PARAM;
        }

        unset($param['sign']);
        ksort($param);
        $str = implode('', $param);
        $sign = strtolower(md5(strtolower(md5($str)) . conGame::SING_PRIVATE_KEY));

        if (strcmp($signClient, $sign) !== 0) {
            Log::error(__METHOD__ . ', invalid sign, signClient = ' . $signClient . ', sign = ' . $sign);
            return conError::ERR_INVALID_SIGN;
        }

        return conError::ERR_OK;
    }

}
